<template>
  <div class="department">
    <page-search
      :searchFormConfig="searchFormConfig"
      @queryBtnClick="handleQueryClick"
      @resetBtnClick="handleResetClick"
    ></page-search>
    <page-content
      ref="pageContentRef"
      :contentTableConfig="contentTableConfig"
      pageName="department"
      @newBtnClick="handleNewData"
      @editBtnClick="handleEditData"
    >
    </page-content>
    <page-modal
      :modalConfig="modalConfigRef"
      :defaultInfo="defaultInfo"
      pageName="department"
      ref="pageModalRef"
    ></page-modal>
  </div>
</template>

<script lang="ts">
import { computed, defineComponent } from 'vue'
import PageContent from '@/components/page-content'
import PageModal from '@/components/page-modal'
import PageSearch from '@/components/page-search'

import { contentTableConfig } from './config/content.config'
import { modalConfig } from './config/modal.config'
import { searchFormConfig } from './config/search.config'

import { usePageModal } from '@/hooks/use-page-modal'
import { usePageSearch } from '@/hooks/use-page-search'

import { useStore } from '@/store'

export default defineComponent({
  name: 'department',
  components: {
    PageContent,
    PageModal,
    PageSearch
  },
  setup() {
    //获取contentRef、重置、搜索操作
    const [pageContentRef, handleResetClick, handleQueryClick] = usePageSearch()

    // 2.动态添加部门
    const store = useStore()
    const modalConfigRef = computed(() => {
      const departmentItem = modalConfig.formItems.find(
        (item) => item.field === 'parentId'
      )
      departmentItem!.options = store.state.entireDepartment.map((item) => {
        return { title: item.name, value: item.id }
      })
      return modalConfig
    })

    const newCallback = () => {
      modalConfig.title = '新增部门'
    }
    const editCallback = () => {
      modalConfig.title = '编辑部门'
    }

    const [pageModalRef, defaultInfo, handleNewData, handleEditData] =
      usePageModal(newCallback, editCallback)
    return {
      contentTableConfig,
      searchFormConfig,
      modalConfigRef,
      pageModalRef,
      defaultInfo,
      handleNewData,
      handleEditData,
      pageContentRef,
      handleResetClick,
      handleQueryClick
    }
  }
})
</script>

<style scoped></style>
